# coding:utf-8
# from __future__ import unicode_literals
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.conf import settings
from os.path import basename
from os.path import join

from django import forms
from django.db import connection
cursor=connection.cursor()
print cursor

from django.contrib.auth.decorators import login_required,permission_required

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

from dj.models import users

@login_required(login_url=settings.LOGIN_URL)
@permission_required('dj.index')
def index(request):
	username = users.objects.values('name')
	# print username
	print request.GET
	if 'name' in request.GET:

		u=request.GET['name']
		l=users.objects.filter(name=u).values()
		print l
		return render_to_response('index.html', {'user':request.user,'l':l[0],'users':username, 'perms':list(request.user.get_all_permissions()), })

	print ('perm', request.user.get_all_permissions())
	return render_to_response('index.html', {'user':request.user,'users':username, 'perms':list(request.user.get_all_permissions()), })


import settings
def login_user(request):

	print request.user.is_authenticated()
	print request.user.has_perm('dj.index')
	page = request.GET.get('next')
	username = request.POST.get('username', '')
	password = request.POST.get('password', '')
	print username,password
	user = authenticate(username=username, password=password)
	print user
	if user is not None:
		# the password verified for the user
		if user.is_active:
			login(request, user)
			if not page:
				# log.info('Module:view.login_user 登录用户:%s 跳转页面:%s' % (user, 'index'))
				return HttpResponseRedirect('/index')
			else:
				# log.info('Module:view.login_user 登录用户:%s 跳转页面:%s' % (user, page))
				return HttpResponseRedirect(page)
		else:
			# the authentication system was unable to verify the username and password
			# log.info('Module:view.login_user 登录用户 密码不正确')
			return render_to_response('login.html', {'login_err': u'用户名 或 密码 错误'})

	return render_to_response('login.html', locals())
